package com.android.myj.view;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.myj.R;
import com.android.utils.AsyncImageLoader;
import com.android.utils.TimeUtil;
import com.android.utils.AsyncImageLoader.ImageCallback;
import com.tencent.weibo.api.Friends_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.WeiBoConst;

public class FansIdol extends ListActivity implements OnItemClickListener {

	private Handler handler;
	private AsyncImageLoader asyncImageLoader;
	private FansThread thread;
	private ListView listView;
	private ProgressDialog progressDialog;
	private JSONArray array;
	private FansAdapter adapter;
	private String name;
	private String currentNick;// ��ǰ������ǳ�
	private View top_panel;
	private TextView top_title;
	SharedPreferences preferences;
	String tag;
	OAuth oauth;
	Friends_API friendsAPI;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fans_idol);
		setUpViews();// ����view
		setUpListeners();// ����listenter
		setToken();
		asyncImageLoader = AsyncImageLoader.getInstance();
		Intent intent = getIntent();
		name = intent.getStringExtra("name");// ��ȡ��ǰ��ҳ�洫�ݹ��������
		currentNick = intent.getStringExtra("nick");
		tag = intent.getStringExtra("tag");
		top_title.setText(currentNick + tag);

		progressDialog = new ProgressDialog(FansIdol.this);// ���һ�������
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle("请稍等");
		progressDialog.setMessage("正在读取数据中!");

		handler = new FansHandler();
		thread = new FansThread();
		thread.start();// ����һ���̻߳�ȡ���
		progressDialog.show();
	}

	private void setUpViews() {
		listView = getListView();
		top_panel = (View) findViewById(R.id.fans_top);
		top_title = (TextView) top_panel.findViewById(R.id.top_title);
	}

	private void setUpListeners() {
		listView.setOnItemClickListener(this);
	}

	class FansThread extends Thread {
		@Override
		public void run() {
			friendsAPI = new Friends_API();
			String jsonStr;
			try {
				if (tag.equals("的粉丝")) {
					 jsonStr = friendsAPI.user_fanslist(oauth,
								WeiBoConst.ResultType.ResultType_Json, "10", "0", name);
				}else {
					jsonStr = friendsAPI.user_idollist(oauth,
							WeiBoConst.ResultType.ResultType_Json, "10", "0", name);
				}
				
				JSONObject dataObj = new JSONObject(jsonStr)
						.getJSONObject("data");
				array = dataObj.getJSONArray("info");
			} catch (Exception e) {
				e.printStackTrace();
			}
			// ֪ͨhandler�������
			Message msg = handler.obtainMessage();
			handler.sendMessage(msg);
		}
	}

	class FansHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			adapter = new FansAdapter(FansIdol.this, array);
			listView.setAdapter(adapter);
			progressDialog.dismiss();// �رս����
		}
	}

	class FansAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private JSONArray array;

		public FansAdapter(Context context, JSONArray array) {
			this.array = array;
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return array.length();
		}

		@Override
		public Object getItem(int position) {
			return array.opt(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			asyncImageLoader = AsyncImageLoader.getInstance();
			FansViewHolder viewHolder = new FansViewHolder();
			JSONObject data = (JSONObject) array.opt(position);
			convertView = inflater.inflate(R.layout.fans_idol_list_item, null);

			viewHolder.fans_headicon = (ImageView) convertView
					.findViewById(R.id.fans_headicon);
			viewHolder.fans_nick = (TextView) convertView
					.findViewById(R.id.fans_nick);
			viewHolder.fans_name = (TextView) convertView
					.findViewById(R.id.fans_name);

			Drawable cachedImage = null;
			if (data != null) {
				try {
					convertView.setTag(data.get("name"));
					viewHolder.fans_nick.setText(data.getString("nick"));
					viewHolder.fans_name.setText("@" + data.getString("name"));
					// �첽����ͼƬ
					cachedImage = asyncImageLoader.loadDrawable(data
							.getString("head")
							+ "/100", viewHolder.fans_headicon,
							new ImageCallback() {
								@Override
								public void imageLoaded(Drawable imageDrawable,
										ImageView imageView, String imageUrl) {
									imageView.setImageDrawable(imageDrawable);
								}
							});
					if (cachedImage == null) {
						viewHolder.fans_headicon
								.setImageResource(R.drawable.icon);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return convertView;
		}
	}

	static class FansViewHolder {
		private ImageView fans_headicon;
		private TextView fans_nick;
		private TextView fans_name;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Intent intent = new Intent(FansIdol.this, UserInfo.class);
		try {
			JSONObject fansInfo = (JSONObject) array.opt(position);
			JSONArray tweets = fansInfo.getJSONArray("tweet");
			JSONObject tweet = null;
			if (tweets != null && tweets.length() > 0) {
				tweet = (JSONObject) tweets.opt(0);
				intent.putExtra("origtext", tweet.getString("text"));
				intent.putExtra("timestamp", TimeUtil.getStandardTime(tweet
						.getLong("timestamp")));
			}
			intent.putExtra("name", fansInfo.getString("name"));
			intent.putExtra("nick", fansInfo.getString("nick"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		startActivity(intent);// ��ת���û���Ϣ����
	}

	private void setToken() {
		preferences = getSharedPreferences("default_user",
				Activity.MODE_PRIVATE);
		String userName = preferences.getString("user_default_nick", "");
		oauth = new OAuth();
		oauth = OAuth.setToken(FansIdol.this, userName);
	}
}
